package cn.hk.message.service.impl;

import cn.hk.message.domain.Message;
import cn.hk.message.mapper.MessageAppuserRecordMapper;
import cn.hk.message.mapper.MessageMapper;
import cn.hk.message.service.MessageAppuserRecordService;
import cn.hk.message.service.MessageCopy20220720Service;
import cn.hk.message.service.MessageService;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 消息推送信息表 服务实现类
 * </p>
 *
 * @author xu
 * @since 2023-03-08
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Autowired
    private MessageAppuserRecordService messageAppuserRecordService;

    @Autowired
    private MessageAppuserRecordMapper messageAppuserRecordMapper;

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private MessageCopy20220720Service messageCopy20220720Service;
    @Override
    public List<Message> getAppUserMessageList(Long userId) {
        //存放message
        List<Message> messageList = new ArrayList<>();
        List<Long> messageIdList = messageAppuserRecordMapper.selectIdsByUserId(userId);

       /* List<MessageAppuserRecord> MessageAppuserRecordList = messageAppuserRecordService
                .list(new LambdaQueryWrapper<MessageAppuserRecord>().eq(MessageAppuserRecord::getAppUserId, userId).select(MessageAppuserRecord::getMessageId));
        List<Long> messageIdList = MessageAppuserRecordList.stream().map(messageAppuserRecord -> messageAppuser getMessageId()).collect(Collectors.toList());*/

        messageIdList.forEach(messageId -> {
            //获取从 message和messageCopy中的消息主体
            List<Message> messageListOne =  messageMapper.selectListByMessageId(messageId);
//            List<Message> messageListOne = baseMapper.selectList(new LambdaQueryWrapper<Message>().eq(Message::getId, messageId));
            List<Message> messageCopyListTwo = messageCopy20220720Service.getMessageList(messageId);

            messageList.addAll(messageListOne);
            messageList.addAll(messageCopyListTwo);
        });
        return messageList;
    }

    @Override
    public List<Message> getIndexMessageList() {
        ArrayList<Message> resultList = CollUtil.newArrayList();
        List<Message> messages = messageMapper.selectIndexMessageList();
        for (Message message : messages) {
            if(message.getImgUrl() != null){
                resultList.add(message);
            }
        }
        return resultList;
    }

    @Override
    public Message getMessageByMessageId(Long messageId) {
        return messageMapper.selectOneByMessageId(messageId);
    }
}
